import TokenUtil from "../common/TokenUtil";
import Constants from '@/common/constants.js';
import {setCurrenTenantId,getCurrenTenantId} from '@/common/constants.js';
export default {
    mounted() {
        // #ifdef H5
        let pages = getCurrentPages();
        if(!pages || pages.length==0){
            return;
        }
        let route= pages[pages.length - 1].route;
        let page = pages[pages.length - 1].$page;

        if(page){
            var title = page.meta.navigationBar.titleText;
            let i18nTitle= this.i18nRouterName(route,title);

            if("pages/list/CustomForm" ==route){
                //使用表单方案时，以在线配置的表单名称为标题
                return;
            }
            page.meta.navigationBar.titleText = i18nTitle;
            if (Constants.TypeH5 === 'H5') {
                document.title = i18nTitle
            }
        }
        // #endif
    },
    methods: {
        /**
         * 解析路由配置名称
         * @param route
         * @param text
         * @param key
         * @returns {*}
         */
        i18nRouterName(route,text){
            //return text;
            let defaultLan= this.$i18n.locale||this._vueAppLanguage;
            if(!defaultLan){
                return text;
            }
            if(!route){
                return text;
            }
            if(route.indexOf('/')>-1){
                route=route.replaceAll('/','.')
            }

            let lang= this.$i18n.messages[defaultLan];
            let routeVal = this.$i18nJson(lang.routerPagesLang,route, text);
            if(!lang || !routeVal){
                return text;
            }
            return routeVal;
        },
        imageUrl(fileId) {
            let   token=TokenUtil.getToken()
            return this.realUrl() + "/api-system/system/core/sysFile/download/" + fileId + "?accessToken=" + token;
        },
        realUrl(){
            let realUrl = ''
            // #ifdef H5
            realUrl= location.origin+Constants.BASE_URL;
            // #endif
            // #ifdef APP-PLUS
            realUrl=Constants.APP_BASE_URL;
            // #endif
            return realUrl;
        },
        setBaseTenantId(tenantId){
            setCurrenTenantId(tenantId);
        },
        getBaseTenantId(){
            return getCurrenTenantId();
        },
        getI18nTitleVal(item,text){
            let title= this.$i18(item.i18nJson,item[text])||item[text];
            return title;
        },
        // 获取路由参数onload不能使用时候调用
        routes(){
            let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
            let curParam = routes[routes.length - 1].$options; //获取路由参数
            return curParam
        },
        clickNavLeft(index){
            let canNavBack = getCurrentPages()
            if( canNavBack && canNavBack.length>1) {
                if (index){
                    if(canNavBack.length<index){
                        uni.switchTab({
                            url: "/pages/RxWorkbench"
                        })
                    }else {
                        uni.navigateBack({
                            delta: index
                        })
                    }
                }else {
                    uni.navigateBack()
                }
            } else if(index) {
                uni.switchTab({
                    url: "/pages/RxWorkbench"
                })
            }else{
                history.back();
            }
        }
    }
}
